/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ik.pbs.Manejadores;

import com.ik.pbs.Encapsulados.Pais;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author alexis
 */
public class ManejadorPais extends Manejador {

    public static ManejadorPais manager = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");
    private SimpleDateFormat shf = new SimpleDateFormat("HH:mm:ss");

    public static ManejadorPais getInstance() {
        if (manager == null) {
            manager = new ManejadorPais();
        }

        return manager;
    }

    public void ManejadorPais() {
    }

    public ArrayList<Pais> getPaises(Connection con) {
        ArrayList<Pais> arregloPais = new ArrayList();

        ResultSet rd = null;
        PreparedStatement state;

        String query = "select * from pais order by id";

        try {
            state = con.prepareCall(query);
            rd = state.executeQuery();

            while (rd.next()) {
                Pais pais = new Pais();
                pais.setId(rd.getInt("id"));
                pais.setDescripcion(rd.getString("descripcion"));

                arregloPais.add(pais);
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return arregloPais;
    }

    public Pais getPais(Connection con, int codigo) {
        Pais pais = null;

        ResultSet rd = null;
        PreparedStatement state;

        String query = "select * from pais where id = ?";

        try {
            state = con.prepareCall(query);
            state.setInt(1, codigo);
            rd = state.executeQuery();

            while (rd.next()) {
                pais = new Pais();
                pais.setId(rd.getInt("id"));
                pais.setDescripcion(rd.getString("descripcion"));
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return pais;
    }

    public boolean insertarPais(Connection con, Pais pais) {
        PreparedStatement state;

        String query = "insert into pais values(?,?)";
        try {


            state = con.prepareStatement(query);
            state.setInt(1, pais.getId());
            state.setString(2, pais.getDescripcion());

            state.execute();
            return true;
        } catch (SQLException ex) {
            return actualizarPais(con, pais);
        }


    }

    public boolean actualizarPais(Connection con, Pais pais) {
        PreparedStatement state;

        String query = "update pais set descripcion = ? where id = ?";

        try {
            state = con.prepareStatement(query);
            state.setInt(2, pais.getId());
            state.setString(1, pais.getDescripcion());

            state.execute();
            return true;

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            return false;
        }


    }

    public int getNext(Connection con) {
        try {
            ResultSet rd = null;
            PreparedStatement state;
            String query = "select max(id) as max from pais";

            state = con.prepareStatement(query);
            rd = state.executeQuery();

            if (!rd.next()) {
                return 1;
            } else {
                return (rd.getInt("max") + 1);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return 0;
    }
}